package com.jiajia.study.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 *
 * 字节输入流输出流的demo
 *
 * 处理非文本文件的时候，就不能用字符流来处理了
 * @author : zjj
 * @date : 2021-04-16 10:50
 */
public class FileInputStreamOutputStreamClient {

    public static void main(String[] args) {
        test2("test.mp4","beauty.mp4");

    }

    /**
     * 测试将文件拷贝到另外一个文件
     */
    private static void test2(String sourceFile,String targetFile){
        File sourcePic = new File(sourceFile);
        File targetPic = new File(targetFile);
        // 字节输入流
        FileInputStream fileInputStream = null;
        // 字符输出流
        FileOutputStream fileOutputStream = null;
        try{
            fileInputStream = new FileInputStream(sourcePic);
            fileOutputStream = new FileOutputStream(targetPic);

            byte[] buffer = new byte[10];
            int len ;
            while ((len = fileInputStream.read(buffer)) > -1){
                fileOutputStream.write(buffer,0,len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                try{
                    fileInputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try{
                    fileOutputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }




}
